home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / plotting / imagetoo / imagetl1.lha / Imagetool / src+obj / new.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-20  |  13.8 KB  |  563 lines

  1. /*
  2. ******************************************************************************
  3. *                               NCSA ImageTool 1.1 beta
  4. *                           Thu Sep 20 16:58:25 CDT 1990
  5. * NCSA ImageTool 1.1 beta source code and documentation are in the public
  6. * domain.
  7. * Specifically, we give to the public domain all rights for future licensing
  8. * of the source code, all resale rights, and all publishing rights.
  9. * We ask, but do not require, that the following message be included in all
  10. * derived works:
  11. * Portions developed at the National Center for Supercomputing Applications at
  12. * the University of Illinois at Urbana-Champaign.
  13. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  14. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  15. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  16. ******************************************************************************
  17. */
  18. /* cat > headers/new.h << "EOF" */
  19. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  20. /* new.h: header for new.c file                */
  21. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  22. /* SCCS information: %W%    %G% - NCSA */
  23.  
  24. #include "all.h"
  25. #include "newext.h"
  26.  
  27. /* EOF */
  28. /* cat > src+obj/new/check_filetype.c << "EOF" */
  29. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  30. /* check_filetype: check whether a file is a file or a  */
  31. /*           directory is a directory.        */
  32. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  33. /* SCCS information: %W%    %G% - NCSA */
  34.  
  35. /* #include "all.h" */
  36. /* #include "newext.h" */
  37.  
  38. int
  39. check_filetype (path, type)
  40.                 /* returns: 0 - correct type
  41.                        -1 - wrong type */
  42.     char           *path;
  43.                 /* input: must be full legal pathname */
  44.     int             type;
  45.                 /* input: TYPE_DIR  - directory
  46.                       TYPE_FILE - file */
  47. {
  48.         /* check if wrong type */
  49.     if (type == TYPE_DIR)
  50.     {        /* directory */
  51.         sprintf (wkstr, "csh -fc \"test -d '%s'\"", path);
  52.         if (check_system (wkstr))    /* 0 exit status means true */
  53.             return (-1);
  54.     }
  55.     else if (type == TYPE_FILE)
  56.     {        /* file */
  57.         sprintf (wkstr, "csh -fc \"test -f '%s'\"", path);
  58. /* printf */
  59. /* msg_write (wkstr); */
  60.         if (check_system (wkstr))    /* 0 exit status means true */
  61.             return (-1);
  62.     }
  63.     return (0);
  64. }
  65. /* EOF */
  66. /* cat > src+obj/new/check_system.c << "EOF" */
  67. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  68. /* check_system: send a command to the system () C    */
  69. /*         library function and return success or    */
  70. /*         failure. Standard error is redirected    */
  71. /*         to the bit bucket.            */
  72. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  73. /* SCCS information: %W%    %G% - NCSA */
  74.  
  75. /* #include "all.h" */
  76. /* #include "newext.h" */
  77.  
  78. int 
  79. check_system (s)
  80.                 /* returns  0 = success
  81.                        -1 = failure */
  82.     char           *s;
  83.                 /* input: command string */
  84. {
  85. /* Implementation note(s):
  86.     1. Use /dev/null as the bit bucket.
  87. */
  88.  
  89.     int             newd;
  90.  
  91.         /* stderr (fd = 2) should go to /dev/null */
  92.     newd = dup (2);
  93.     close (2);
  94.  
  95.     (void) open ("/dev/null", O_WRONLY);    /* should not fail - opens on 2 */
  96.  
  97.     if (system (s))    
  98.     {
  99.         (void) dup2 (newd, 2);
  100.         close (newd);
  101.         return (-1);
  102.     }
  103.     else
  104.     {        /* 0 exit means true */
  105.         (void) dup2 (newd, 2);
  106.         close (newd);
  107.         return (0);
  108.     }
  109. }
  110. /* EOF */
  111. /* cat > src+obj/new/first_word.c << "EOF" */
  112. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  113. /* first_word: get first word in a string. White space    */
  114. /*           is space or tab.                */
  115. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  116. /* SCCS information: %W%    %G% - NCSA */
  117.  
  118. /* #include "all.h" */
  119.  
  120. char           *
  121. first_word (s, len)
  122.                 /* returns: pointer to first character of word */
  123.     char           *s;
  124.                 /* input: string to locate first word */
  125.     int            *len;
  126.                 /* returns: length of word in characters */
  127. {
  128.     register int    c;
  129.  
  130.         /* strip off any leading white space */
  131.     *len = 0;
  132.     while ((c = *s++) != NULL)
  133.         if (c != ' ' && c != '\t')
  134.         {
  135.             (*len)++;
  136.             break;
  137.         }
  138.  
  139.         /* scan the word */
  140.     while ((c = *s++) != NULL)
  141.         if (c == ' ' || c == '\t')
  142.             break;
  143.         else
  144.             (*len)++;
  145.  
  146.     return (s - *len - 1);
  147. }
  148. /* EOF */
  149. /* cat > src+obj/new/free_list.c << "EOF" */
  150. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  151. /* free_list: free storage associate with a char **    */
  152. /*          variable from wildcard.            */
  153. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  154. /* SCCS information: %W%    %G% - NCSA */
  155.  
  156. /* #include "all.h" */
  157. /* #include "newext.h" */
  158.  
  159. int 
  160. free_list (var)
  161.     char          **var;
  162. {
  163.     int i;
  164.  
  165.         /* if there are no files - var points to one location with
  166.            a NULL pointer */
  167.     if (var[0] == NULL)
  168.         return;
  169.     else
  170.     {
  171.         i = 0;
  172.         while (var[i] != NULL)
  173.             free (var[i++]);
  174.     }
  175.     free (var);
  176.     return;
  177. }
  178. /* EOF */
  179. /* cat > src+obj/new/get_abspathname.c << "EOF" */
  180. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  181. /* get_abspathname: resolve a path to an absolute     */
  182. /*            pathname. The intended type, file    */
  183. /*            or directory must be provided.    */
  184. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  185. /* SCCS information: %W%    %G% - NCSA */
  186.  
  187. /* #include "all.h" */
  188. /* #include "newext.h" */
  189.  
  190. int 
  191. get_abspathname (fn, maxfnlen, type)
  192.                 /* returns:
  193.                     error - 0 = no error
  194.                         1 = no such type
  195.                         2 = wrong type
  196.                         3 = no pathname
  197.                         4 = multiple pathnames
  198.                         5 = ambiguous type - wildcard 
  199.                         6 = syntax - wildcard (error 1)
  200.                         7 = memory allocation -
  201.                             wildcard (error 2) -
  202.                             internal
  203.                         8 = not enough string space in
  204.                             argument - internal
  205.                         9 = pathname for current
  206.                             directory failed - internal
  207.                            10 = change directory failed
  208.                             - internal
  209.                            11 = pathname for new directory
  210.                             failed - internal
  211.                            12 = NULL pointer to fn -
  212.                             internal
  213.                            13 = nonpositive maxfnlen
  214.                             value - internal
  215.                            14 = bad type value - internal */
  216.     char           *fn;
  217.                 /* input: relative or absolute pathname that
  218.                           would be parsed correctly by the
  219.                       C-shell. Multiple pathnames result
  220.                       in an error.
  221.                    returns: (no error) the absolute pathname
  222.                         fully resolved provided the length
  223.                         is not greater than maxfnlen - 1.
  224.                         (error) orginal string is
  225.                         returned. */
  226.     int             maxfnlen;
  227.                 /* input: maximum length of final path + 1 of
  228.                       filename storage for fn. */
  229.     int             type;
  230.                 /* input: TYPE_DIR  - directory
  231.                       TYPE_FILE - file */
  232. {
  233.     int             nt, err, fnlen, fflen, cdlen;
  234.     char          **path;
  235.     char           *fpart, *pwd, *new_pwd;
  236.  
  237.     char           *getcwd ();    /* standard C library */
  238.  
  239.         /* bad arguments */
  240.     if (fn == NULL)
  241.         return (12);
  242.     else if (maxfnlen < 1)
  243.         return (13);
  244.     else if (type < 0 || type > 1)
  245.         return (14);
  246.  
  247.     if ((nt = word_count (fn)) != 1)
  248.     {        /* incorrect number of "tokens" */
  249.         if (nt == 0)
  250.             return (3);    /* no pathname */
  251.         else
  252.             return (4);    /* too many potential paths */
  253.     }
  254.  
  255.     path = wildcard (fn, &err);
  256.  
  257. /* printf */
  258. /* sprintf (wkstr, "path = %s", path[0]);
  259. msg_write (wkstr); */
  260.     if (err != 0)
  261.     {        /* wildcard error */
  262.         if (err == 1)
  263.             return (6);    /* syntax */
  264.         else
  265.             return (7);    /* memory allocation */
  266.     }
  267.     if (path[0] == NULL)    /* no match */
  268.         return (1);
  269.     else if (path[1] != NULL)    /* ambiguous filename */
  270.     {
  271.         (void) free_list (path);
  272.         return (5);
  273.     }
  274.     if ((fnlen = strlen (path[0])) > maxfnlen - 1)    /* not enought string space */
  275.     {
  276.         (void) free_list (path);
  277.         return (8);
  278.     }
  279.  
  280.         /* build full pathname */
  281.     pwd = getcwd ((char *) NULL, MAXNAMELEN + 1);    /* must free before return */
  282.     if (pwd == NULL)    /* size too small or internal error */
  283.     {
  284.         (void) free_list (path);
  285.         return (9);
  286.     }
  287.     cdlen = strlen (pwd);
  288.     if (type == TYPE_FILE)    /* file */
  289.     {
  290.         fpart = strrchr (path[0], '/');    /* find last / */
  291.         if (fpart == NULL)    /* no directory part - use current directory */
  292.         {        /* add 1 for / */
  293.             if ((cdlen + 1 + fnlen) > maxfnlen - 1)    /* not enough string space */
  294.             {
  295.                 (void) free_list (path);
  296.                 free (pwd);
  297.                 return (8);
  298.             }
  299.             sprintf (wkstr2, "%s/%s", pwd, path[0]);
  300. /* printf */
  301. /* msg_write (wkstr2); */
  302.             if (check_filetype (wkstr2, type))
  303.             {
  304.                 (void) free_list (path);
  305.                 free (pwd);
  306.                 return (2);
  307.             }
  308.             strcpy (fn, wkstr2);
  309.             (void) free_list (path);
  310.             free (pwd);
  311.             return (0);
  312.         }
  313.         fflen = strlen (fpart) - 1;
  314.         *fpart = '\0';    /* temporary make path[0] point just to the directory part */
  315.     }
  316.     if (chdir (path[0]))    /* if problem then internal error */
  317.     {
  318.         (void) free_list (path);
  319.         free (pwd);
  320.         return (10);
  321.     }
  322.     new_pwd = getcwd ((char *) NULL, MAXNAMELEN + 1);    /* must free before return */
  323.     if (new_pwd == NULL)    /* size too small or internal error */
  324.     {
  325.         (void) free_list (path);
  326.         free (pwd);
  327.         return (11);
  328.     }
  329.     cdlen = strlen (new_pwd);
  330.     if (type == TYPE_DIR)
  331.     {        /* directory */
  332.         strcpy (wkstr2, new_pwd);
  333.         if (check_filetype (wkstr2, type))
  334.         {
  335.             (void) free_list (path);
  336.             free (pwd);
  337.             free (new_pwd);
  338.             return (2);
  339.         }
  340.         strcpy (fn, new_pwd);
  341.     }
  342.     else if (type == TYPE_FILE)    /* file */
  343.     {        /* add 1 for / */
  344.         if ((cdlen + 1 + fflen) > maxfnlen - 1)
  345.         {        /* not enough string space */
  346.             (void) free_list (path);
  347.             (void) chdir (pwd);    /* should not error */
  348.             free (pwd);
  349.             free (new_pwd);
  350.             return (8);
  351.         }
  352.         sprintf (wkstr2, "%s/%s", new_pwd, fpart + 1);
  353.         if (check_filetype (wkstr2, type))
  354.         {
  355.             (void) free_list (path);
  356.             free (pwd);
  357.             free (new_pwd);
  358.             return (2);
  359.         }
  360.         strcpy (fn, wkstr2);
  361.     }
  362.  
  363.     (void) free_list (path);
  364.     (void) chdir (pwd);    /* should not error */
  365.     free (pwd);
  366.     free (new_pwd);
  367.     return (0);
  368. }
  369. /* EOF */
  370. /* cat > src+obj/new/pixrect_pad.c << "EOF" */
  371. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  372. /* pixrect_pad: horizontal padding in bytes for memory  */
  373. /*        pixrect.                */
  374. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  375. /* SCCS information: %W%    %G% - NCSA */
  376.  
  377. /* #include "all.h" */
  378. /* #include "newext.h" */
  379.  
  380. int
  381. pixrect_pad (xdim)
  382.                 /* returns: the number of bytes of
  383.                         horizontal padding */
  384.     int             xdim;
  385.                 /* input: horizontal dimension of memory
  386.                       pixrect */
  387. {
  388. /*
  389.    BUG: mpr_linebytes () is wrong on Sun-3 and Sun-4. It computes padding
  390.         to 16 bits not 32 bits which mem_create uses. Decided not to use it.
  391.     Can't tell when it works!
  392. */
  393. #if sun == sparc || sun == mc68020 || sun == mc68030
  394.     return ((xdim % 4 == 0) ? 0 : 4 - xdim % 4);
  395. #else
  396.     return ((xdim % 2 == 0) ? 0 : 2 - xdim % 2);
  397. #endif
  398. }
  399. /* EOF */
  400. /* cat > src+obj/new/strip_wspace.c << "EOF" */
  401. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  402. /* strip_wspace: strip off leading and trailing white    */
  403. /*         space.                    */
  404. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  405. /* SCCS information: %W%    %G% - NCSA */
  406.  
  407. /* #include "all.h" */
  408. /* #include "newext.h" */
  409.  
  410. int 
  411. strip_wspace (s)
  412.                 /* returns the length */
  413.     char           *s;
  414.                 /* input: string to strip
  415.                    returns: stripped string with s[0]
  416.                         the first non-whitespace
  417.                         character */
  418. {
  419. /* Implementation note(s):
  420.     1. Assumes that s is not NULL and the string is NULL
  421.        terminated.
  422. */
  423.  
  424.     register int    i, j, k;
  425.  
  426.         /* strip off trailing white space */
  427.     if ((i = strlen (s)) == 0)
  428.         return (0);
  429.     while (--i >= 0 && (s[i] == ' ' || s[i] == '\t'));
  430.     s[++i] = '\0';
  431.     if (i == 0)
  432.         return (0);
  433.  
  434.         /* strip off leading white space */
  435.     if (s[0] != ' ' && s[0] != '\t')
  436.         return (i);
  437.     for (j = 0; s[j] == ' ' || s[j] == '\t'; j++);
  438.     for (k = 0; k < i - j; k++)
  439.         s[k] = s[k + j];
  440.     s[i - j] = '\0';
  441.     return ((i - j));
  442. }
  443. /* EOF */
  444. /* cat > src+obj/new/tty_write.c << "EOF" */
  445. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  446. /* tty_write: write message to tty subwindow        */
  447. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  448. /* SCCS information: %W%    %G% - NCSA */
  449.  
  450. /* #include "all.h" */
  451. /* #include "newext.h" */
  452.  
  453. void 
  454. tty_write (msg)
  455.     char           *msg;
  456. {
  457.  
  458.     ttysw_input (ttysw, msg, strlen (msg));
  459. }
  460. /* EOF */
  461. /* cat > src+obj/new/window_corner.c << "EOF" */
  462. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  463. /* window_corner: get corner coordinates of frame     */
  464. /*          PIXWIN                */
  465. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  466. /* SCCS information: %W%    %G% - NCSA */
  467.  
  468. /* #include "all.h" */
  469. /* #include "newext.h" */
  470.  
  471. int
  472. window_corner (frame, vside, hside, x_corner, y_corner)
  473.                 /* returns: 0 = success
  474.                        -1 = failure */
  475.     Frame           frame;
  476.                 /* input: frame to get corner corrdinates of */
  477.     enum rect_vertical vside;
  478.                 /* input: which vertical side for corner */
  479.     enum rect_horizontal hside;
  480.                 /* input: which horizontal side for corner */
  481.     int            *x_corner;
  482.                 /* returns: x value of PIXWIN corner if no error */
  483.     int            *y_corner;
  484.                 /* returns: y value of PIXWIN corner if no error */
  485. {
  486.     Rect           *rect_frame;
  487.  
  488.     if (frame == NULL)
  489.         return (-1);
  490.     rect_frame = (Rect *) window_get (frame, WIN_RECT);
  491.  
  492.     *x_corner = rect_frame->r_left;
  493.     *y_corner = rect_frame->r_top;
  494.     switch (vside)
  495.     {
  496.         case LEFT:
  497.             switch (hside)
  498.             {
  499.                 case TOP:
  500.                     break;
  501.                 case BOTTOM:
  502.                     *y_corner += rect_frame->r_height;
  503.                     break;
  504.                 default:
  505.                     return (-1);
  506.             }
  507.             break;
  508.         case RIGHT:
  509.             *x_corner += rect_frame->r_width;
  510.             switch (hside)
  511.             {
  512.                 case TOP:
  513.                     break;
  514.                 case BOTTOM:
  515.                     *y_corner += rect_frame->r_height;
  516.                     break;
  517.                 default:
  518.                     return (-1);
  519.             }
  520.             break;
  521.         default:
  522.             return (-1);
  523.     }
  524.     return (0);
  525. }
  526. /* EOF */
  527. /* cat > src+obj/new/word_count.c << "EOF" */
  528. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  529. /* word_count: number of words in string. White space    */
  530. /*           is space or tab.                */
  531. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  532. /* SCCS information: %W%    %G% - NCSA */
  533.  
  534. /* #include "all.h" */
  535. /* #include "newext.h" */
  536.  
  537. int 
  538. word_count (s)
  539.                 /* returns: the number of words in
  540.                         the input string */
  541.     char           *s;
  542. {
  543.     register int    count, word, c;
  544.  
  545.     count = 0;
  546.     word = 0;
  547.     while ((c = *s++) != NULL)
  548.         if (c == ' ' || c == '\t')    /* white space */
  549.             word = 0;
  550.         else if (!word)
  551.         {        /* not white space */
  552.             word = 1;
  553.             count++;
  554.         }
  555.     return (count);
  556. }
  557. /* EOF */
  558.